//
//  NRLSVMTSubjectViewController.m
//  NaRiLSVMT
//
//  Created by jmsre on 14-8-9.
//  Copyright (c) 2014年 helloyouth.org. All rights reserved.
//
#import "NRLSVMTSubjectViewController.h"
#import "UIViewController+MJPopupViewController.h"
#import "SettingViewController.h"
#import "FlowControlViewController.h"
#import "NRLSVMTSubjectViewController+Request.h"
#import "NRLSVMTViewController.h"
#import "FlowDetailViewController.h"
extern  UIImageView *subjectShadowImageView;   //主题按钮选中阴影

extern  dispatch_source_t timer_playFlowScreen;//播放定时器

extern  NSMutableArray  *subjectButtonArr;//主题两个按钮数组

extern  BOOL    shutDownSetup;

@interface NRLSVMTSubjectViewController ()<SettingPopupDelegate>
{
    SettingViewController *settingViewController;
}
@end
@implementation NRLSVMTSubjectViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.navigationController.navigationBar addSubview:self.navigationView];
    
    [AddNoticeObserver(@"NetworkSignalNotice"){
        NSNumber    *num = [note object];
        if(num.integerValue == 1)
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                self.signalButton.selected = YES;
                isHaveDisconnect = YES;
            });
        }
        if(num.integerValue == 0)
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                self.signalButton.selected = NO;
                isHaveDisconnect = NO;
            });
        }
    }];
    // Do any additional setup after loading the view from its nib.
    [self NRLSMTGetFlowForPad];//查询流程
    TopSubjectView *topScrollView = [TopSubjectView shareInstance];
    topScrollView.delegate = self;
    self.scrollView.contentOffset = CGPointMake(0,0);
    self.scrollView.contentSize = CGSizeMake(Screen_Width,Screen_Height - 64);
    [self.view addSubview:self.scrollView];
    [self.scrollView setBackgroundColor:ColorRGB(25,76,86)];
    self.rootScreenPreview = [[ScreenPreviewView alloc] initWithFrame:CGRectMake(0,0,Screen_Width,Screen_Height-64)];
   // self.rootScreenPreview.backgroundColor = [UIColor redColor];
    self.rootScreenPreview.delegate = self;
    
    topScrollView.nameArray = @[@"屏幕预览", @"流程控制"];
    
    self.rootScreenPreview.frame = CGRectMake(0,0,Screen_Width,Screen_Height-64);
    
    [self.chooseView addSubview:topScrollView];
    
    [self.scrollView addSubview:self.rootScreenPreview];
    
    [topScrollView initWithNameButtons];
    
    /************设置网络信号(signalButton)按钮状态************/
    [self.signalButton setBackgroundImage:[UIImage imageNamed:@"subjectTopsignal_a@2x.png"] forState:UIControlStateNormal];
    [self.signalButton setBackgroundImage:[UIImage imageNamed:@"subjectTopsignal_b@2x.png"] forState:UIControlStateSelected];
    
    [AddNoticeObserver(NoticeKey_ScrollViewThird){
       self.detailView = [note object];
       self.detailView.view.frame = CGRectMake(Screen_Width*2,0,Screen_Width,Screen_Height);
       [self.scrollView addSubview:self.detailView.view];
       self.scrollView.contentOffset = CGPointMake(Screen_Width*2,0);
     
    }];
    [AddNoticeObserver(NoticeKey_ScrollViewSecond){
        self.detailView = nil;
        self.scrollView.contentOffset = CGPointMake(Screen_Width,0);
    }];
    
    /***************************************************/
}
- (void)dealloc {
    RemoveNoticeObserver(@"NetworkSignalNotice", self);
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}


- (IBAction)setupAction:(id)sender {
    settingViewController = nil;
    settingViewController = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];
    settingViewController.delegate = self;
    settingViewController.isLogin = YES;
    //[self presentViewController:settingViewController animated:NO completion:nil];
    [self presentPopupViewController:settingViewController animationType:MJPopupViewAnimationFade];
}

- (void)backButtonClicked:(SettingViewController *)setupViewController
{
    if(settingViewController.isLogin){
       [self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationFade];
        settingViewController = nil;
        return;
    }
    NRLSVMTViewController   *loginViewController = [[NRLSVMTViewController alloc] initWithNibName:@"NRLSVMTViewController" bundle:nil];
    [loginViewController deviceisRegister:1];
    [loginViewController testUuidlaw];
    settingViewController = nil;
    
    [self presentViewController:loginViewController animated:NO completion:nil];
    
    if (shutDownSetup == YES) {
        return;
    }
   // self.navigationController.navigationBar.hidden = YES;
    
    
    /***********************在这里把所有前一次连接的数据都要清除***********************/
    for (UIView *v in self.chooseView.subviews) {
        if ([v isKindOfClass:[UIView class]]) {
            [v removeFromSuperview];
        }
    }
    if (timer_playFlowScreen != nil) {
        dispatch_source_cancel(timer_playFlowScreen);
    }
    for(UIButton *but in subjectButtonArr)
    {
        but.selected = NO;
    }
    for(UIView *v in self.chooseView.subviews)
    {
        if ([v isKindOfClass:[UIButton class]])
        {
            [v removeFromSuperview];
        }
    }
    /**********************************************/
    
    [subjectShadowImageView removeFromSuperview];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark-
#pragma mark- TopSubjectViewDelegate
- (void)HandlePushViewController:(id)viewController
{
    self.scrollView.contentOffset = CGPointMake(Screen_Width,0);
   // NSLog(@"%f  %f",self.rootScreenPreview.frame.origin.x,self.rootScreenPreview.frame.origin.y);
}

- (void)HandlePopViewController:(id)viewController
{
    self.scrollView.contentOffset = CGPointMake(0,0);
    //NSLog(@"%f  %f",self.scrollView.frame.origin.x,self.scrollView.frame.origin.y);
    self.rootScreenPreview.frame = CGRectMake(0,64,Screen_Width,Screen_Height);
    if(self.detailView){
        PostNoticeObserver(NoticeKey_BackrootView,nil);
    }
}

- (void)HandlePopTwiceViewController:(id)viewController
{
    self.scrollView.contentOffset = CGPointMake(0,0);
    self.rootScreenPreview.frame = CGRectMake(0,64,Screen_Width,Screen_Height);
}
#pragma -mark
#pragma -mark screenpreviewDelegate
-(void)presentForscreen:(UIViewController *)collen
{
    [self.navigationController presentViewController:collen animated:YES completion:nil];
    self.rootScreenPreview.frame = CGRectMake(0,0,Screen_Width,Screen_Height);
}
@end
